package cn.gjxblog.modules.sys.controller;

import cn.gjxblog.common.model.HttpResult;
import cn.gjxblog.common.utils.captcha.GoogleCaptchaUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;

/**
 * @Author guojiaxun
 * @Date 2019/9/19 9:12
 */
@RestController
public class SysLoginController {

    /**
     * 验证码
     */
    @GetMapping("captcha.jpg")
    public void captcha(HttpServletResponse response, HttpSession session) throws IOException {
        //生成code
        String text = GoogleCaptchaUtils.createText();
        session.setAttribute("captcha_code",text);
        //获取图片验证码
        BufferedImage image  = GoogleCaptchaUtils.creteImage(text);

        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image,"jpg", out);

        IOUtils.closeQuietly(out);
    }

    @PostMapping("/login")
    public HttpResult login(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", "4344323121398");

        return HttpResult.ok(map);
    }
}
